package me.zhengjie.modules.system.domain

import cn.hutool.core.bean.BeanUtil
import cn.hutool.core.bean.copier.CopyOptions
import com.fasterxml.jackson.annotation.JsonTypeName
import com.fasterxml.jackson.databind.ser.Serializers.Base
import io.swagger.v3.oas.annotations.media.Schema
import jakarta.persistence.*
import jakarta.validation.constraints.NotBlank
import jakarta.validation.constraints.NotNull
import jakarta.validation.constraints.Pattern
import me.zhengjie.common.base.BaseEntity
import me.zhengjie.common.utils.JacksonUtil
import org.hibernate.annotations.CreationTimestamp
import org.springframework.data.redis.core.RedisHash
import java.sql.Date
import java.sql.Timestamp

/**
 * @author Kuki Wu
 * @date 2024-11-22
 */

@Entity
@Table(name = "sys_user")
@JsonTypeName("sysuser")
@RedisHash("sysuser")
data class User(
    @Id
    @Column(name = "user_id")
    @Schema(description = "用户ID")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    var id: @NotNull(groups = [Update::class]) Long? = null,

    @ManyToMany( fetch = FetchType.EAGER)
    @Schema(description = "用户角色")
    @JoinTable(
        name = "sys_users_roles",
        joinColumns = [JoinColumn(name = "user_id", referencedColumnName = "user_id")],
        inverseJoinColumns = [JoinColumn(name = "role_id", referencedColumnName = "role_id")]
    )
    var roles: Set<Role>? =null,

    @ManyToMany(fetch = FetchType.EAGER)
    @Schema(description = "用户岗位")
    @JoinTable(
        name = "sys_users_jobs",
        joinColumns = [JoinColumn(name = "user_id", referencedColumnName = "user_id")],
        inverseJoinColumns = [JoinColumn(name = "job_id", referencedColumnName = "job_id")]
    )
    var job: Set<Job>? = null,

    @OneToOne
    @Schema(description = "用户部门")
    @JoinColumn(name = "dept_id")
    var dept: Dept? = null,

    @Column(unique = true)
    @Schema(description = "用户名")
    var username: @NotBlank String? = null,

    @Column(unique = true)
    @Schema(description = "用户昵称")
    var nickName: @NotBlank String? = null,

    @Schema(description = "邮箱")
    var email: @NotBlank @Pattern(
            regexp = "([a-z0-9A-Z]+[-|.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}",
            message = "格式错误"
        ) String? = null,

    @Schema(description = "电话号码")
    var phone: @NotBlank String? = null,

    @Schema(description = "性别")
    var gender: @NotBlank String? = null,

    @Schema(description = "头像真实名称", hidden = true)
    var avatarName: @NotBlank String? = null,

    @Schema(description = "头像存储的路径", hidden = true)
    var avatarPath: String? = null,

    @Schema(description = "是否启用：1启用、0禁用")
    var enabled: @NotNull Boolean? = null,

    @Schema(description = "密码")
    var password: String? = null,

    @Schema(description = "是否为admin账号", hidden = true)
    private var isAdmin: Boolean? = false,

    @Column(name = "pwd_reset_time")
    @Schema(description = "修改密码的时间")
    var pwdResetTime: Date? = null,

): BaseEntity(){
    annotation class Update

    companion object {
        fun fromJson(json: String): User? {
            val obj = JacksonUtil.parseObject(json, User::class.java)!!
            return when (obj) {
                is User -> obj
                else -> null
            }
        }
    }

    fun copy(source: User?) {
        BeanUtil.copyProperties(source, this, CopyOptions.create().setIgnoreNullValue(true))
    }
}